package com.ccp.dev.extend.controller;

import com.ccp.dev.core.basic.annotion.Action;
import com.ccp.dev.core.basic.consts.UrlConstants;
import com.ccp.dev.core.basic.internationalization.SysBaseController;
import com.ccp.dev.core.basic.model.SysAuditModelType;
import com.ccp.dev.core.util.RequestUtil;
import com.ccp.dev.core.util.log.SysAuditThreadLocalHolder;
import com.ccp.dev.extend.service.ExtendSysFileService;
import com.ccp.dev.system.model.SysFile;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * 扩展的SYS_FILE 控制器类
 *
 * @author WYL
 */
@RestController
//@Action(ownermodel = SysAuditModelType.SYSTEM_SETTING)
@RequestMapping(UrlConstants.PROJECT_ROOT + "/extend/attachFile")
public class ExtendSysFileController extends SysBaseController {
    @Resource
    private ExtendSysFileService extendSysFileService;
    /**
     * 附件下载
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     */
    @GetMapping("/download")
    @Action(description = "附件下载", exectype = "下载日志",
            detail = "<#assign entity=sysFileService.getOneById(fileId)/>下载附件【${entity.fileName}】<#if success>成功<#else>失败</#if>")
    public void downloadFileS(HttpServletRequest request, HttpServletResponse response) throws Exception {
        String fileId = RequestUtil.getString(request, "fileId");
        extendSysFileService.downloadFile(request, response, fileId);
        SysAuditThreadLocalHolder.putParamerter("success", true);
    }

    @RequestMapping("/update")
    @ResponseBody
    public Object updateFileLevel(@RequestBody SysFile[] files) throws Exception{
        int result=extendSysFileService.updateFileLevel(files);
        return result > 0 ? getResultDataSuccess("save.success") : getResultDataError("save.failure");
    }
}
